/**
 * Created by wangxiaodan on 2017/4/21.
 */
$(function(){
   
    $('.treeview-menu li').click(function(){//点击li加载界面
        var url = $(this).attr('data-url');
        $(this).siblings().removeClass('active');
        $(this).addClass('active');
        getContent(url);
    });
});

//跳转按钮事件
function jumpfun(obj) {
    getContent($(obj).attr('data-url'));
}

//提交查询按钮
function postfun(obj) {
    $.post($(obj).attr("data-url"), $('form').serialize(), function (data) {
        $(".content").html(data);
    });
}

//提交查询按钮
function getfun(obj) {
    $.get($(obj).attr("data-url"), $('form').serialize(), function (data) {
        $(".content").html(data);
    });
}


//提交按钮事件
function subAction(obj) {
    $.ajax({
        url: $(obj).attr("data-url"),
        type: $(obj).attr("data-type"),
        data: $('form').serialize(),
        dataType: "json",
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success: function (data) {
            if(data.code == 1){
                swal("操作成功!", "", "success");
                getContent(data.url);
            }else {
                swal("操作失败!", data.msg, "error");
            }
        },
        error: function (msg) {
            console.log(msg);
            var err = JSON.parse(msg.responseText);

            var error = "";
            for (var i in err){
                error += '<span style="color: #f27474;">' + err[i] + '<span>' + '<br />';
                console.log(err[i]);
                console.log(error);
            }
            error = error.substring(0, error.length-6);
            swal({
                title: "错误",
                text: error,
                html: error,
                type: "error",
            });
        }
    });
}


function getContent(url) {
    $.get(url,function(data){
        if(data.status>0){
            swal(
                '错误',
                data.msg,
                'error'
            );
            return false;
        }
        $(".content").html(data);
    });
}

function delfun(obj) {
    swal({
            title:'确认清空此单页缓存么？',
            text:'',
            type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonText: '确认'
        }).then(function(){
        $.ajax({
            url: $(obj).attr('data-url'),
            type: "DELETE",
            dataType: "json",
            success: function(data) {
                if(data.code == 1){
                    swal("操作成功!", "", "success");
                    // $(obj).parent().parent().remove();
                }else {
                    if(data.msg){
                        swal("操作失败!", data.msg, "error");
                    }else{
                        swal("操作失败!", '数据错误', "error");
                    }
                }
            },
            error:function () {
                swal('无此单页缓存','','error');
            }
        });
    });
}

function delimgfun(obj) {
    swal({
            title:'确认删除此图片吗？',
            text:'',
            type:'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            cancelButtonText: '取消',
            confirmButtonText: '确认'
        }).then(function(){
        $.ajax({
            url: $(obj).attr('data-url'),
            type: "DELETE",
            dataType: "json",
            success: function(data) {
                if(data.code == 1){
                    swal("操作成功!", "", "success");
                    getContent(data.url);
                    // $(obj).parent().parent().remove();
                }else {
                    if(data.msg){
                        swal("操作失败!", data.msg, "error");
                    }else{
                        swal("操作失败!", '数据错误', "error");
                    }
                }
            },
            error:function () {
                swal('删除失败','','error');
            }
        });
    });
}

